package com.evil.activiti.config;

import org.activiti.engine.impl.cfg.IdGenerator;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

@Configuration
public class ProcessEngineConfigurator {

    @Resource
    private UUIDGenerator uuidGenerator;

    @Bean
    public ProcessEngineConfigurationImpl processEngineConfigurationImpl(ProcessEngineConfigurationImpl processEngineConfigurationImpl) {
        //设置ProcessEngineConfigurationImpl里的uuidGenerator
        processEngineConfigurationImpl.setIdGenerator(uuidGenerator);
        //设置DbSqlSessionFactory的uuidGenerator，否则流程id，任务id，实例id依然是用DbIdGenerator生成
        processEngineConfigurationImpl.getDbSqlSessionFactory().setIdGenerator(uuidGenerator);
        return processEngineConfigurationImpl;
    }

    @Component
    static class UUIDGenerator implements IdGenerator {
        @Override
        public String getNextId() {
            return UUID.randomUUID().toString().replaceAll("-", "");
        }
    }
}
